import {
  conn_house_data,
} from '~/database/socketData.js'
export const wsSend = (conn, data) => {
  // console.log('发出的ws数据：' + JSON.stringify(data))
  if (conn && conn.readyState == 1) {
    // console.log('conn的链接状态:', conn.readyState, 'conn的链接状态结束')
    conn.sendText(JSON.stringify(data))
  }
}
export const sleep = time => new Promise(resolve => setTimeout(resolve, time))
// 给房间每个人发送ws
export const house_every_send_conn = (house_id, data) => {
  if (conn_house_data[house_id]) {
    const members = conn_house_data[house_id].members;
    for (const openid in members) {
      const conn = conn_house_data[house_id].members[openid].conn
      wsSend(conn, data)
    }
  }
}
// 从大到小排序
export const sort = (data) => {
  for (var i = 1; i < data.length; i++) {
    // 获取未排序集合中第一个元素
    var temp = data[i];
    var j = i;
    // 依次与已排序集合中元素比较，找到正确位置
    while (j > 0 && temp.score > data[j - 1].score) {
      data[j] = data[j - 1];
      j--;
    }
    // 取到的元素放入已排序列表中正确位置
    data[j] = temp;
  }
  return data;
}
export const RandomNumBoth = (Min, Max) => {
  var Range = Max - Min;
  var Rand = Math.random();
  var num = Min + Math.round(Rand * Range); // 四舍五入
  return num;
}
